package cn.edu.nju.software.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import CommonInformation.GoodsInformation;
import CommonInformation.PurchaseInformation;

public class DetailSystemInterfaceImpl extends UnicastRemoteObject 
implements DetailSystemInterface{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3982084877843003189L;
	private String driver;
	private String url;
	private String user;
	private String password;

	protected DetailSystemInterfaceImpl() throws RemoteException {
		super();
		// TODO Auto-generated constructor stub
		driver = "com.mysql.jdbc.Driver";
		url = "jdbc:mysql://localhost/test";
		user = "root";
		password = "NICAIA";
	}

	@Override
	public ArrayList<GoodsInformation> inquireGoods(String goodName, int 
			minPrice, int maxPrice)throws RemoteException {
		// TODO Auto-generated method stub
		try {
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			Statement statement = conn.createStatement(); 
			String sql = "select * from goodslist " +
					"where GoodsName='" + goodName + "'";
			ResultSet rs = statement.executeQuery(sql);
			ArrayList<GoodsInformation> al = new ArrayList<GoodsInformation>();
			GoodsInformation gi = null;
			while(rs.next()) {
				gi = new GoodsInformation();
				gi.setID(rs.getInt(1));
				gi.setName(rs.getString(2));
				gi.setPrice(rs.getDouble(4));
				gi.setDescription(rs.getString(6));
				gi.setSellerName(String.valueOf(rs.getInt(5)));
				al.add(gi);
			}
			rs.close();
			conn.close();
			System.out.println("al");
			return al;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("bl");
		return null;
	}

	@Override
	public boolean submitRequest(int ID, int goodsNum)
			throws RemoteException {
		// TODO Auto-generated method stub
		try {
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			Statement statement = conn.createStatement(); 
			String sql = "select * from goodslist " +
					"where GoodsID='" + ID + "'";
			ResultSet rs = statement.executeQuery(sql);
			rs.next();
			int realNum = rs.getInt(3);
			if (goodsNum <= realNum) {
				return true;
			} else {
				return false;
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public PurchaseInformation confirmPurchase(int goodsID, 
			String time, String address, String type, 
			int num) throws RemoteException {
		// TODO Auto-generated method stub
		try {
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			Statement statement = conn.createStatement(); 
			String sql = "select * from goodslist " +
					"where GoodsID='" + goodsID + "'";
			ResultSet rs = statement.executeQuery(sql);
			rs.next();
			int temp = rs.getInt(3);
			double unitPrice = rs.getDouble(4);
			int sellerID = rs.getInt(5);
			double totalPrice = unitPrice * num;
			temp = temp - num;
			sql = "update goodlist set GoodsNum='" + temp + 
			"' where GoodsID='"+ goodsID + "'";
			statement.execute(sql);
			sql = "insert into transactionslist (UserID, GoodsID, TotalPrice, " +
					"State) values (" + goodsID + ", " + 
					totalPrice + ", '0'";
			statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
			rs = statement.getGeneratedKeys();
			int transactionsID = 0;
			if (rs.next()) {
				transactionsID = rs.getInt(1);
			}
			PurchaseInformation pi = new PurchaseInformation();
			pi.setID(transactionsID);
			pi.setTotalPrice(totalPrice);
			pi.setSellerID(sellerID);
			return pi;
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
